<?php
function displaypost($mysqli, $postID) {  // Function called to display a single post along with its comments
    $commentcounter=1;
    if ($stmt = $mysqli->prepare("SELECT content,UNIX_TIMESTAMP(post_date) FROM `posts` WHERE `ID` = ? AND `visible` =1")){
    $stmt->bind_param('i',$postID);
    $stmt->execute();
    $stmt->bind_result($content,$timestamp);
    $stmt->fetch();
	$content=stripslashes($content);
        echo '<div class="post"><p>',nl2br($content),'</p><div class="info"><div class="leftinfo">#',$postID,' (<a href="?post=',$postID,'">comments</a>)</div><div class="rightinfo">',date('g:i:s A l F jS Y',$timestamp),'</div></div></div>';
        $stmt->close();
        if ($stmt = $mysqli->prepare("SELECT commentID,commentText,UNIX_TIMESTAMP(comment_date) FROM `comments` WHERE `postID` = ? AND `visible` =1")){
            $stmt->bind_param('i',$postID);
            $stmt->execute();
            $stmt->store_result();
            $rows = $stmt->num_rows;
            $stmt->bind_result($commentID,$commentText,$timestamp);
            while ($stmt->fetch()){
		        $commentText=stripslashes($commentText);
                echo '<div class="post"><p>',nl2br($commentText),'</p><div class="info"><div class="leftinfo">Comment #',$commentcounter,' of ',$rows,'</div><div class="rightinfo">',date('g:i:s A l F jS Y',$timestamp),'</div></div></div>';
                $commentcounter++;                      	            
    	    }
    	    $stmt->close();
        }
    }
    echo ('<p>Post a comment:</p>');
    $formtype="comment";
    require_once ('form.php');
    echo '</div></div>';
}

function displayallposts($mysqli) {
    $formtype="content";
    require_once ('form.php');
    $stmt = $mysqli->prepare("SELECT ID,content,UNIX_TIMESTAMP(post_date),comment_count FROM `posts` WHERE `visible` =1 ORDER BY `ID` DESC");
    $stmt->execute();
    $stmt->bind_result($postID,$content,$timestamp,$comment_count);
    while ($stmt->fetch()){
	    $content=stripslashes($content);
        echo '<div class="post"><p>',nl2br($content),'</p><div class="info"><div class="leftinfo">#',$postID,' (<a href="?post=',$postID,'">',$comment_count,' comments</a>)</div><div class="rightinfo">',date('g:i:s A l F jS Y',$timestamp),'</div></div></div>';
    }
    echo '</div></div>';
}
?>

